/**    
* @Title: SsoUserHolderFilter.java  
* @Package com.kaver.sso.filter   
* @author roc   
* @date Aug 11, 2021 2:58:09 PM  
* @version V1.0  
* @copyright kaver 
*/
package com.kaver.sso.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;

import com.kaver.sso.def.resp.User;
import com.kaver.sso.helper.CookieUtil;
import com.kaver.sso.helper.SsoHelper;
import com.kaver.sso.helper.SsoUserHolder;

/**  
* @ClassName: SsoUserHolderFilter  
* @Description: 用户信息保存
* @author roc  
* @date Aug 11, 2021 2:58:09 PM  
*    
*/
@Order(-100)
public class SsoUserHolderFilter implements Filter{
    @Autowired
	SsoHelper ssoHelper;
	
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        try {
            HttpServletRequest req = (HttpServletRequest)request;
            String sessionId = CookieUtil.getValue(req, "session-id");
            if (!StringUtils.isEmpty(sessionId)) {
                User user = ssoHelper.checkSessionId(sessionId);
                if (user != null) {
                    SsoUserHolder.set(user);
                }
            }
            chain.doFilter(request, response);
        } finally {
            SsoUserHolder.remove();
        }
    }
    @Override
    public void destroy() {
        
    }
    
}
